package com.sxz.sbs.applibrary.component;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.support.v7.widget.AppCompatTextView;
import android.util.AttributeSet;
import com.sxz.sbs.applibrary.R;
/**
 * 标签TextView
 */

public class LabelTextView extends AppCompatTextView {
    /**
     * 填充颜色
     */
    private int fillColor;
    /**
     * 边框圆半径
     */
    private int borderRadius;
    /**
     * 边框宽度
     */
    private int borderWidth;
    /**
     * 边框颜色
     */
    private int borderColor;
    /**
     * 左内间距
     */
    private int leftPadding;
    /**
     * 上内间距
     */
    private int topPadding;
    /**
     * 右内间距
     */
    private int rightPadding;
    /**
     * 下内间距
     */
    private int bottomPadding;

    /**
     * 要设置的背景drawable
     */
    private GradientDrawable gradientDrawable;
    private Context context;
    public LabelTextView(Context context) {
        this(context,null);
        this.context = context;
    }
    public LabelTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        TypedArray t = context.obtainStyledAttributes(attrs, R.styleable.LabelTextView);
        fillColor = t.getColor(R.styleable.LabelTextView_fill_color, Color.WHITE);
        borderRadius = t.getInteger(R.styleable.LabelTextView_border_radius,3);
        borderWidth = t.getInteger(R.styleable.LabelTextView_border_width,0);
        borderColor = t.getColor(R.styleable.LabelTextView_border_color, Color.WHITE);
        leftPadding = t.getInteger(R.styleable.LabelTextView_left_padding,8);
        topPadding = t.getInteger(R.styleable.LabelTextView_top_padding,3);
        rightPadding = t.getInteger(R.styleable.LabelTextView_right_padding,8);
        bottomPadding = t.getInteger(R.styleable.LabelTextView_bottom_padding,3);
        t.recycle();
        init();
    }

    /**
     * 初始化
     */
    private void init() {
        gradientDrawable = new GradientDrawable();
        gradientDrawable.setColor(fillColor);
        gradientDrawable.setCornerRadius(dp2px(borderRadius));
        gradientDrawable.setStroke(borderWidth, borderColor);
        setPadding(leftPadding,topPadding,rightPadding,bottomPadding);
        setBackgroundDrawable(gradientDrawable);
        /*int colors[] = { 0xff255779 , 0xff3e7492, 0xffa6c0cd };//分别为开始颜色，中间夜色，结束颜色
        GradientDrawable gd = new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM, colors);*/
    }
    /**
     * 设置背景颜色
     * @param color
     */
    public void setBackGround (int color) {
        this.fillColor = color;
        gradientDrawable.setColor(color);
        setBackgroundDrawable(gradientDrawable);
    }
    /**
     * dp转px
     *
     * @param dpValue dp值
     * @return px值
     */
    public  int dp2px(final float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }
}
